using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace SoftwareBase.Handler
{
    public static class FileManager
    {
        private static string SavePath => Path.Join(Environment.CurrentDirectory, "Result");

        public async static Task SaveFileAsync(string fileName, IFormFile file)
        {
            using var stream = new FileStream(Path.Join(SavePath, fileName), FileMode.Create);
            await file.CopyToAsync(stream);
        }

        public static void CompressFile(string fileName)
        {
            var source = Path.Join(SavePath, fileName);
            var to = Path.Join(SavePath, Path.GetFileNameWithoutExtension(fileName));
            if (Directory.Exists(to))
            {
                Directory.Delete(to, true);
            }
            ZipFile.ExtractToDirectory(source, to);
        }

      

        public static string ReadFile(string fileName)
        {
            using var reader = new StreamReader(Path.Join(Environment.CurrentDirectory, fileName));
            var result = reader.ReadToEnd();
            return result;
        }
    }
}